function Firezoom()
{
  this.zoom = 1;
  this.func = null;
  this.func2 = null;
  this.steps = 5;
  this.czoom = 1;
  this.delta = 0;
  this.target = null;
  this.time = 100;
  this.Y = 0;
}

Firezoom.prototype.zoomTo = function() {
  this.zoom = 1;
  this.czoom = 1;
  this.target = null;
  window._content.document.removeEventListener("click", this.func, false);
  this.func = this.closure('onClick');
  window._content.document.addEventListener("click", this.func, false);
  window._content.document.body.style.cursor = "-moz-zoom-in";
};

Firezoom.prototype.onClick = function(event) {
  window._content.document.removeEventListener("click", this.func, false);
  var target = event.originalTarget;
  while(!this.isZoomable(target.tagName)){
    target = target.parentNode;
  }
  var w = target.offsetWidth;
  var W = window._content.document.documentElement.clientWidth;
  var zoom = 0.8 * W/w;
  var Y = event.clientY;
  if(zoom > 1){
    this.zoom = zoom;
    this.delta = this.zoom - this.czoom;
    this.target = target;
    this.Y = Y;
    this.doZoom();
  }
  window._content.document.body.style.cursor = "default";

  window._content.document.removeEventListener("dblclick", this.func2, false);
  this.func2 = this.closure('onDbClick');
  window._content.document.addEventListener("dblclick", this.func2, false);
  event.preventDefault();
};

Firezoom.prototype.onDbClick = function(event) {
  window._content.document.removeEventListener("dblclick", this.func2, false);
  var target = event.originalTarget;
  if(this.czoom > 1){
    this.zoom = 1;
    this.target = target;
    this.delta = this.zoom - this.czoom;
    this.doZoom();
  }
  event.preventDefault();
};

Firezoom.prototype.doZoom = function() {
  if(this.czoom < this.zoom) {
    var newzoom = this.czoom + this.delta / this.steps;
    if(newzoom < this.zoom){
      this.zoomIt(newzoom, this.target, this.Y);
      this.czoom = newzoom;
      setTimeout(this.closure2('doZoom'), this.time/this.steps);
    }
    else{
      this.zoomIt(this.zoom, this.target, this.Y);
      this.czoom = this.zoom;
    }
  }else {
    var newzoom = this.czoom + this.delta / this.steps;
    if(newzoom > this.zoom){
      this.zoomIt(newzoom, this.target, this.Y);
      this.czoom = newzoom;
      setTimeout(this.closure2('doZoom'), this.time/this.steps);
    }
    else{
      this.zoomIt(this.zoom, this.target, this.Y);
      this.czoom = this.zoom;
    }
  }
};

Firezoom.prototype.zoomIt = function(zoom, target, y) {
  var docViewer = getBrowser().selectedBrowser.markupDocumentViewer;
  docViewer.fullZoom = zoom;
  if(zoom > this.czoom)
    docViewer.scrollToNode(target);
    
};

Firezoom.prototype.isZoomable = function(tagname) {
  var name_array = new Array("table", "tr", "pre", "p", "div", "img", "ul", "ol" ,"dl","h1", "h2","h3" , "textarea", "span", "embed", "object", "body", "html");
  return name_array.indexOf(tagname.toLowerCase()) != -1;
};

Firezoom.prototype.closure = function(method)
{
  var obj = this;
  var func = function(param){
    return obj[method](param);
  };
  return func;
};

Firezoom.prototype.closure2 = function(method)
{
  var obj = this;
  var func = function(param1, param2){
    return obj[method](param1, param2);
  };
  return func;
};

var fireZoom = new Firezoom();